package com.cxrj.zhsq.propertyManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.propertyManage.dao.ReleaseMapper;
import com.cxrj.zhsq.propertyManage.entity.Release;
import com.cxrj.zhsq.propertyManage.service.ReleaseService;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.utils.HtmlUtil;
import com.cxrj.zhsq.utils.IpUtil;
import com.cxrj.zhsq.utils.UploadController;

@Service
public class ReleaseServiceImpl implements ReleaseService {

	@Resource
	private ReleaseMapper releaseMapper;

	@Resource
	private AppUserMapper appUserMapper;

	@Override
	public Map<String, Object> selectReleasePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总条数
		String recordsTotal = "0";

		String kod = request.getParameter("kod");

		String sqlStr = " where 1=1 ";

		if (kod != null && !kod.equals("")) {
			sqlStr += "and release_name like " + '"' + '%' + kod + '%' + '"' + " or release_account like " + '"' + '%' + kod + '%' + '"';
		}

		sqlMap.put("condition", sqlStr);
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		recordsTotal = releaseMapper.getReleaseCount(sqlStr) + "";
		List<Map<String, Object>> releaseList = releaseMapper.selectReleasePage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", releaseList);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectById(String appealId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		Release release = releaseMapper.selectByPrimaryKey(appealId);
		if (release.getFeedbackContent() != null && !release.getFeedbackContent().equals("")) {
			release.setFeedbackContent(HtmlUtil.getAbsSource(release.getFeedbackContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		resultMap.put("release", release);
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		if ("1".equals(release.getReleaseState())) {
			Release release2 = new Release();
			release2.setAppealId(release.getAppealId());
			release2.setReleaseState("2");
			releaseMapper.updateByPrimaryKeySelective(release2);
		}

		return resultMap;
	}

	@Override
	public Map<String, Object> updateRelease(Release release) {
		Map<String, Object> resultMap = new HashMap<>();
		int a = releaseMapper.updateByPrimaryKeySelective(release);
		resultMap.put("result", "-1");
		if (a > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteRelease(String appealId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			releaseMapper.deleteByPrimaryKey(appealId);
			UploadController.deleteDir(appealId, request, response);
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertRelease(String userId, String appContent, String pics) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		Release release = new Release();
		release.setAppealId(UUID.randomUUID().toString());
		release.setReleaseAccount(appUser.getUserAccount());
		release.setReleaseName(appUser.getUserName());
		release.setReleaseContent(appContent);
		if (!pics.equals("")) {
			release.setReleaseImg(pics.substring(1));
		}
		release.setCreateTime(new Date());
		release.setReleaseState("1");
		Integer c = releaseMapper.insertSelective(release);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectRelease(String status, String userAccount, Integer dataIndex, Integer pageCount) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("releaseAccount", userAccount);
		String type = "";
		if (status.equals("2")) {
			type = "1,2,3";
		} else {
			type = "4";
		}
		sqlMap.put("releaseState", type);
		sqlMap.put("start", dataIndex);
		sqlMap.put("length", pageCount);
		List<Release> list = releaseMapper.selectRelease(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}
}
